package com.mrzhou.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mrzhou.core.tool.api.R;
import com.mrzhou.core.tool.constants.CloudConstant;
import com.mrzhou.core.tool.utils.BeanUtil;
import com.mrzhou.core.tool.utils.Func;
import com.mrzhou.mybatis.support.Condition;
import com.mrzhou.system.entity.Dict;
import com.mrzhou.system.req.PageDictReq;
import com.mrzhou.system.service.IDictService;
import com.mrzhou.system.vo.DictDetailVO;
import com.mrzhou.system.vo.DictListVO;
import com.mrzhou.system.vo.DictParentVO;
import com.mrzhou.system.vo.DictVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典controller
 */
@RestController
@RequestMapping("dict")
public class DictController {

    @Autowired
    private IDictService dictService;

    @PostMapping("page")
    @ApiOperation("分页查询字典列表")
    public R<IPage<DictListVO>> listPage(PageDictReq query) {
        // 只对字典顶级分页
        IPage<Dict> dictPage = dictService.page(new Page<>(query.getCurrent(), query.getSize()), Condition.getQueryWrapper(query, Dict.class).eq(Dict::getParentId, CloudConstant.TOP_PATENT_ID));

        // 构建返回值, 获取各个顶级字典的子类集合
        IPage<DictListVO> page = new Page<>(dictPage.getCurrent(), dictPage.getSize(), dictPage.getTotal());
        if (!Func.isListEmpty(dictPage.getRecords())) {
            List<DictListVO> vos = dictPage.getRecords().stream().map(parentDict -> {
                DictListVO vo = BeanUtil.copy(parentDict, DictListVO.class);
                // 获取对应的字典
                vo.setChildren(BeanUtil.converList(dictService.list(new LambdaQueryWrapper<Dict>().eq(Dict::getParentId, vo.getId())), DictListVO.class));
                return vo;
            }).collect(Collectors.toList());
            page.setRecords(vos);
        }
        return R.data(page);
    }

    @PostMapping("detail")
    @ApiOperation("字典详情")
    public R<Dict> detail(@RequestParam Long id) {
        Dict detail =  dictService.getById(id);
        if (null == detail) {
            return R.fail("未知的的字典id");
        }
        // 获取父级字典的部分信息
        DictDetailVO detailVO = BeanUtil.copy(detail, DictDetailVO.class);
        if (!detailVO.getParentId().equals(CloudConstant.TOP_PATENT_ID)) {
            Dict parentDict = dictService.getOne(new LambdaQueryWrapper<Dict>().select(Dict::getDictCode, Dict::getDictKey).eq(Dict::getId, detail.getParentId()));
            detailVO.setParentDictKey(parentDict.getDictKey());
            detailVO.setParentDictCode(parentDict.getDictCode());
        } else {
            detailVO.setParentDictCode(detail.getDictCode());
            detailVO.setParentDictKey(detail.getDictKey());
        }
        return R.data(detailVO);
    }

    @PostMapping("submit")
    @ApiOperation("增加或者修改")
    public R<String> submit(@RequestBody Dict dict) {
        // 新增时当字典类型为空时, 将parentId设置为0L
        if (Func.hasAllEmpty(dict.getId(), dict.getParentId())) {
            dict.setParentId(CloudConstant.TOP_PATENT_ID);
        }
        return R.status(dictService.saveOrUpdate(dict));
    }

    @PostMapping("get-dict")
    @ApiOperation("获取指定字典对象")
    public R<List<DictVO>> getDict(@RequestParam String dictCode) {
        List<Dict> list = dictService.getDictByCode(dictCode);
        return R.data(BeanUtil.converList(list, DictVO.class));
    }

    @PostMapping("get-parent-dict")
    @ApiOperation("获取系统目前的字典类型")
    public R<List<DictParentVO>> getParentDict() {
        List<Dict> list = dictService.list(new LambdaQueryWrapper<Dict>().eq(Dict::getParentId, CloudConstant.TOP_PATENT_ID));
        return R.data(BeanUtil.converList(list, DictParentVO.class));
    }


}
